
$(function() {
  slideFade($('#banner-slide'));
  headerSearh($('#header-search'));
  quertAction($('#query'));
});



function slideFade(dom) {
  if(dom.length <1) {
    return ;
  }

  var liDom = dom.find('li');
  liDom.css('opacity','0').hide();
  liDom.eq(0).show().animate({opacity:'1'}, 400);
  var len = liDom.length;
  if(len > 1) {
    var pot = '<li class="selected"></li>';
    for(var i = len-1; i--;){
      pot += '<li></li>'
    }
    $('<ul>').html(pot).attr('id','spot-box').appendTo(dom);

    dom.find('#spot-box li').on('click', function(){
      var that = $(this);
      var index = that.index();
      liDom.css('opacity','0').hide();
      liDom.eq(index).stop().show().animate({opacity:'1'}, 400);
      that.addClass('selected');
      that.siblings().removeClass('selected');
    });

    dom.find('#prev').on('click', function(){
      var index = liDom.filter(':visible').index();
      liDom.eq(index).css('opacity','0').hide();
      if(index == 0){
        index = len;
      }
      liDom.eq(index-1).stop().show().animate({opacity:'1'}, 400);
      var that = dom.find('#spot-box li').eq(index-1);
      that.addClass('selected');
      that.siblings().removeClass('selected');
    });

    dom.find('#next').on('click', function(){
      var index = liDom.filter(':visible').index();
      liDom.eq(index).css('opacity','0').hide();
      if((len - index) == 1){
        index = -1
      }
      liDom.eq(index+1).stop().show().animate({opacity:'1'}, 400);
      var that = dom.find('#spot-box li').eq(index+1);
      that.addClass('selected');
      that.siblings().removeClass('selected');
    });

    //添加定时器
    setInterval(function(){
      var nextDom = dom.find('#next');
      if(!nextDom.is(':visible')){
        nextDom.trigger('click');
      }
    }, 10000);

  }
};


function headerSearh(dom) {
  if(dom.length <1) {
    return ;
  }

  dom.on('click', '#btn-ok', function() {
    $keyword = dom.find('[name=keyword]');
    var keyword = $keyword.val();
    var type = $keyword.attr('search-type');
     window.location.href = '/' + type + '/search?keyword=' + $.trim(keyword);
    return false;
  });
}


function quertAction(dom) {
  if(dom.length <1) {
    return ;
  }

  dom.on('click', '#query-city', function() {
    var fromCityName = $.trim(dom.find('#fromCityName').val());
    var toCityName = $.trim(dom.find('#toCityName').val());
    $.post('/query', {fromCityName:fromCityName, toCityName:toCityName}, function(data) {
      if(data) {
        dom.find('#results').html(data);
      }
    });
    return false;
  });

  dom.on('click', '#query-fno', function() {
    var fno = $.trim(dom.find('#fno').val());
    $.post('/query', {fno:fno}, function(data) {
      if(data) {
        dom.find('#results').html(data);
      }
    });
    return false;
  });


}